package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.SchoolNeeds;
import com.xmy.cultivate.entity.SchoolSort;
import com.xmy.cultivate.entity.Station;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISchoolNeedsService;
import com.xmy.cultivate.service.IStationService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Objects;

/**
 * <p>
 * 校区招聘需求表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/api/schoolNeeds")
public class SchoolNeedsController extends BaseController {

    @Autowired
    ISchoolNeedsService iSchoolNeedsService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStationService iStationService;


    /**招聘需求列表
     *
     *
     * @param pageNum
     * @param pageSize
     * @param schoolOrDepartmentId
     * @param stationId
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "-1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "schoolOrDepartmentId", defaultValue = "") String schoolOrDepartmentId,
                                 @RequestParam(value = "stationId", defaultValue = "") String stationId,
                                 @RequestParam(value = "status", defaultValue = "") String status
                               ) {

        IPage<SchoolNeeds> iPage = new Page<>(pageNum, pageSize);
        iSchoolNeedsService.findAll(iPage,  schoolOrDepartmentId, stationId,  status);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }


    /**添加校区招聘需求*/
    @PostMapping("/addSchoolNeeds")
    public Result addSchoolNeeds(@RequestBody SchoolNeeds schoolNeeds) {
        Station stationOld = iStationService.getById(schoolNeeds.getStationId());
        if (Objects.isNull(stationOld)){
            return new Result(Code.EXP_ERROR, null, "该岗位不存在,添加失败");
        }
        //是否区分科目：0否，1是
        if(stationOld.getIsHasSubjects() == 0){
            schoolNeeds.setSubjectsId(0L);
        }

        QueryWrapper<SchoolNeeds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", schoolNeeds.getStationId());
        if (stationOld.getType() == 1){
            queryWrapper.eq("school_id", schoolNeeds.getSchoolOrDepartmentId());

        } else if(stationOld.getType() == 2){
            queryWrapper.eq("school_id", 1600466723094175746L);
        }
        queryWrapper.eq("subjects_id", schoolNeeds.getSubjectsId());

        long count = iSchoolNeedsService.count(queryWrapper);
        if (count > 0){
            return new Result(Code.ERROR, null, "该岗位已添加过该招聘需求,请勿重复添加");
        }
        //1为校区招聘岗位，2为部门招聘岗位
        if (stationOld.getType() == 1){
            schoolNeeds.setSchoolId(schoolNeeds.getSchoolOrDepartmentId());

        } else if(stationOld.getType() == 2){
            schoolNeeds.setDepartmentId(schoolNeeds.getSchoolOrDepartmentId());
            schoolNeeds.setSchoolId(1600466723094175746L);
        }
        Quarter quarterNow = iQuarterService.getNowQuarter();
        Integer yearPart = quarterNow.getYearPart();  //年份
        Integer quarterNum = quarterNow.getNum();    //学期
        schoolNeeds.setYearPart(yearPart);
        schoolNeeds.setQuarterNum(quarterNum);
        boolean save = iSchoolNeedsService.save(schoolNeeds);
        if (!save){
            return new Result(Code.EXP_ERROR, null, "添加失败");
        }
        return new Result(Code.OK, null, "添加成功");

    }


    /**编辑校区招聘需求*/
    @PutMapping("/editSchoolNeeds")
    public Result editSchoolNeeds(@RequestBody SchoolNeeds schoolNeeds) {
        Station stationOld = iStationService.getById(schoolNeeds.getStationId());
        if (Objects.isNull(stationOld)){
            return new Result(Code.EXP_ERROR, null, "该岗位不存在,编辑失败");
        }
        SchoolNeeds schoolNeedsOld = iSchoolNeedsService.getById(schoolNeeds.getId());
        if (Objects.isNull(schoolNeedsOld)) {
            return new Result(Code.ERROR,"", "该名称不存在,编辑失败");
        }
        //是否区分科目：0否，1是
        if(stationOld.getIsHasSubjects() == 0){
            schoolNeeds.setSubjectsId(0L);
        }

        QueryWrapper<SchoolNeeds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", schoolNeeds.getStationId());
        queryWrapper.ne("id", schoolNeeds.getId());
        if (stationOld.getType() == 1){
            queryWrapper.eq("school_id", schoolNeeds.getSchoolOrDepartmentId());

        } else if(stationOld.getType() == 2){
            queryWrapper.eq("school_id", 1600466723094175746L);
        }
        queryWrapper.eq("subjects_id", schoolNeeds.getSubjectsId());
        long count = iSchoolNeedsService.count(queryWrapper);
        if (count > 0){
            return new Result(Code.ERROR, null, "该岗位已添加过该招聘需求,请勿重复添加");
        }
        //1为校区招聘岗位，2为部门招聘岗位
        if (stationOld.getType() == 1){
            schoolNeeds.setSchoolId(schoolNeeds.getSchoolOrDepartmentId());
            schoolNeeds.setDepartmentId(null);

        } else if(stationOld.getType() == 2){
            schoolNeeds.setDepartmentId(schoolNeeds.getSchoolOrDepartmentId());
            schoolNeeds.setSchoolId(1600466723094175746L);
        }

        boolean update = iSchoolNeedsService.updateSchoolNeedsInfo(schoolNeeds.getSchoolId(), schoolNeeds.getDepartmentId(), schoolNeeds.getStationId(), schoolNeeds.getSubjectsId(), schoolNeeds.getId(),schoolNeeds.getStatus());
//        boolean update = iSchoolNeedsService.updateById(schoolNeeds);
        if (!update){
            return new Result(Code.EXP_ERROR, null, "编辑失败");
        }
        return new Result(Code.OK, null, "编辑成功");

    }




    @GetMapping()
    public Result getSchoolNeeds(@RequestParam(value = "pageNum", defaultValue = "-1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
                                @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum,
                                @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
                                @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
                                @RequestParam(value = "status", defaultValue = "") String status,// 0 未满  1已满
                                @RequestParam(value = "startDate", defaultValue = "") String startDate,
                                @RequestParam(value = "endDate", defaultValue = "") String endDate) {

        IPage<SchoolNeeds> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<SchoolNeeds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        queryWrapper.orderByAsc("created_at");

        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            queryWrapper.between("DATE(created_at)", startDate, endDate);
        }
        if (StringUtils.isNotBlank(status)) {
            if (status.equals("0")) {

            } else {

            }
        }

        iSchoolNeedsService.page(iPage, queryWrapper);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @PostMapping("/saveSchoolNeeds")
    public Result saveSchoolNeeds(@RequestBody SchoolNeeds schoolNeeds) {
        Quarter quarterNow = iQuarterService.getNowQuarter();
        Integer yearPart = quarterNow.getYearPart();  //年份
        Integer quarterNum = quarterNow.getNum();    //学期
        schoolNeeds.setYearPart(yearPart);
        schoolNeeds.setQuarterNum(quarterNum);
        iSchoolNeedsService.save(schoolNeeds);
        return new Result(Code.OK, "", Code.OK_MSG);
    }

    @PutMapping("/updateSchoolNeeds")
    public Result updateSchoolNeeds(@RequestBody SchoolNeeds schoolNeeds) {
        SchoolNeeds schoolNeedsOld = iSchoolNeedsService.getById(schoolNeeds.getId());
        if (Objects.isNull(schoolNeedsOld)){
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }
        iSchoolNeedsService.updateById(schoolNeeds);
        return new Result(Code.OK, "", Code.OK_MSG);
    }

    /**
     * 修改校区需求
     * @param schoolNeeds
     * @return
     */
    @PutMapping("/updateNeedsCount")
    public Result updateNeedsCount(@RequestBody SchoolNeeds schoolNeeds) {
        SchoolNeeds schoolNeedsOld = iSchoolNeedsService.getById(schoolNeeds.getId());
        if (Objects.isNull(schoolNeedsOld)){
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }
        schoolNeedsOld.setNeedCount(schoolNeeds.getNeedCount());
        iSchoolNeedsService.updateById(schoolNeedsOld);

        return new Result(Code.OK, "", Code.OK_MSG);
    }


    /**
     * 修改校区状态
     * @param schoolNeeds
     * @return
     */
    @PutMapping("/updateNeedsStatus")
    public Result updateNeedsStatus(@RequestBody SchoolNeeds schoolNeeds) {
        SchoolNeeds schoolNeedsOld = iSchoolNeedsService.getById(schoolNeeds.getId());
        if (Objects.isNull(schoolNeedsOld)){
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }
        schoolNeedsOld.setStatus(schoolNeeds.getStatus());
        iSchoolNeedsService.updateById(schoolNeedsOld);

        return new Result(Code.OK, "", Code.OK_MSG);
    }


    @DeleteMapping("")
    public Result deleteSchoolNeeds(@RequestParam(value = "id") String id) {
        SchoolNeeds schoolNeeds = iSchoolNeedsService.getById(id);
        if (Objects.isNull(schoolNeeds)) {
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }
        iSchoolNeedsService.removeById(id);
        return new Result(Code.DELETE_OK, null, Code.DELETE_OK_MSG);
    }

}
